#ifndef __PAC_IO_H__
#define __PAC_IO_H__

#include <cstdint>
#include <driver/i2c_master.h>

class PcaIO
{
public: 
    enum PinNum {
        PIN_IO0 = 0,
        PIN_IO1,
        PIN_IO2,
        PIN_IO3,
        PIN_IO4,
        PIN_IO5,
        PIN_IO6,
        PIN_IO7,
    };
    enum PinValue {
        PIN_UNKNOWN = -1,
        PIN_LOW = 0,
        PIN_HIGH,
    };
    enum PinDir {
        PIN_INPUT = 0,
        PIN_OUTPUT,
    };

    enum PinPolarity {
        PIN_NON_INVERTED = 0,
        PIN_INVERTED,
    };

    #define PCA9557_CONTROL_REG_0 0x00	// Input Port Register           (R)    BXXXXXXXX (Default)
    #define PCA9557_CONTROL_REG_1 0x01	// Output Port Register          (R/W)  B00000000
    #define PCA9557_CONTROL_REG_2 0x02	// Polarity Inversion Register   (R/W)  B11110000
    #define PCA9557_CONTROL_REG_3 0x03	// Configuration Register        (R/W)  B11111111


private:
    /* data */
    i2c_master_dev_handle_t m_i2c_dev;
    uint8_t m_i2c_addr;
public:
    PcaIO (/* args */) {}
    ~PcaIO () {
        deinit();
    }

    void init(i2c_master_bus_handle_t i2c_bus, uint8_t addr = 0x19);
    void deinit();
    int write_pin(int pin, PinValue value);
    PinValue read_pin(int pin);
    int set_pin_dir(int pin, PinDir dir);
    int set_intput_polarity(int pin, PinPolarity polarity);

private:
    int write_reg(uint8_t cmd, uint8_t value);
    int read_reg(uint8_t cmd, uint8_t *value, uint8_t count);
};


#endif // __PAC_IO_H__